package com.spring.boot.rpc.rpc05;

import com.spring.boot.rpc.rpc00.IUserService;
import com.spring.boot.rpc.rpc00.User;

import java.io.*;
import java.lang.reflect.Method;
import java.net.ServerSocket;
import java.net.Socket;

/**
 *
 */
public class Server {
    private static boolean running = true;

    public static void main(String[] args) throws Exception {
        ServerSocket ss = new ServerSocket(8888);
        while (running){
            Socket s = ss.accept();
            process(s);
            s.close();
        }
        ss.close();


    }

    private static void process(Socket s) throws IOException, Exception {
        InputStream in = s.getInputStream();
        OutputStream out = s.getOutputStream();
        ObjectInputStream ois = new ObjectInputStream(in);
        DataOutputStream dos = new DataOutputStream(out);

        String methodName = ois.readUTF();// 读取方法名
        Class[] parameterTypes = (Class[]) ois.readObject();// 读取参数类型
        Object[] args = (Object[])ois.readObject();// 读取参数

        IUserService service = new IUserServiceImpl();
        Method method = service.getClass().getMethod(methodName, parameterTypes);
        User user = (User)method.invoke(service, args);

        /**
         * 改成把对象往外写
         */
        ObjectOutputStream oos = new ObjectOutputStream(out);
        oos.writeObject(user);
        oos.flush();
    }




}
